/**
 * Created by ZFL on 2017/9/22.
 */
var vm=new Vue({
    el:'#xianghong',
    data:{
        earningCords:[],    //收益记录
        shareCords:[],      //份额纪录
        userYestodayFee:0,          //昨日收益
        userTotalFee:0,        //累计收益
        userNum:0,         //我的总分数
        platformNum:0,      //平台总分数
        platformTotalFee:0,   //平台总献金
        platformYestodayFee1000:0,  //平台昨日献金
        tel:'',                //用户手机号
        code:0,            //验证码

    }
})
Vue.filter('price',function(value){
    return divideNumber(value,100);
})
function appInit(){
    PageInit();
}

function PageInit(){
    viewEarnings();
    viewShare();
    getUserTel();
    infiniteSroll();
    //infiniteSrollS();
}
$('.records_tab a').click(function(){
    var _index=$(this).index()+1;
   if(_index==1){
       viewEarnings();
   }else if(_index==2){

       viewShare();

   }
    $(this).addClass("textshow").siblings().removeClass("textshow");
    $(".tab_" + _index + "").addClass("contentshow").siblings().removeClass("contentshow");
});
//点击献金转出按钮
$('.rolloutBtn').click(function(){
    $('.operateBtns').hide();
    $('.rolloutOpe').toggle();
    $('#fullbg').show();
    $('body').css({
        overflow:'hidden'
    })
})
//点击遮罩
function closeRollout(){
    $('.operateBtns').show();
    $('.rolloutOpe').toggle();
    $('#fullbg').hide();
    $('body').css({
        overflow:'auto'
    })
}
var pageNum1=1;
var pageSize1=10;
var loading1=false;
  //查看收益
function viewEarnings(){
    $.ajaxHandler({
        model:function(){
            var obj={};
            obj.userId= $.getPara('userId');
            obj.pageSize=pageSize1;
            obj.pageNum=pageNum1;
            console.log(obj)
            return obj;
        },
        visibleTime: 0,
        dataType: "json",
        visible:'hide',
        api: 'yg_baijiaapporder/orderRebate/selectRebateInfoByUserId',
        success:function(result){
            if(result.code=10000){
                //vm.earningCords=[];

                var data=result.data;
                vm.userNum = data.userNum;   //用户的献金数
                vm.platformNum = data.platformNum;    //平台总献金
                vm.userYestodayFee = data.userYestodayFee;   //昨日收益
                vm.userTotalFee =data.userTotalFee;     //累计收益
                vm.platformYestodayFee1000 = data.platformYestodayFee1000;   //昨日昨日每万份献金净值




                    if(data.list.length>0){
                        //加载数据
                        loading1=false;
                        data.list.forEach(function(value,index){
                            value.money=divideNumber(value.money,100);
                            value.updateTime=value.updateTime.split(' ')[0];
                            if((JSON.stringify(vm.earningCords).indexOf(JSON.stringify(value)))<0){
                                vm.earningCords.push(value);
                            }

                        })
                    }else{
                    //$(document.body).destroyInfinite();
                        loading1=true;
                        //pageNum1=1;
                }








            }else{
                YDUI.dialog.toast(result.message, 'error', 1000);
            }
        }
    })
}

var pageNum2=1;
var pageSize2=10;
var loading2=false;
//查看份额纪录
function viewShare(){
    $.ajaxHandler({
        model:function(){
            var obj={};
            obj.userId= $.getPara('userId');
            obj.pageSize=pageSize2;
            obj.pageNum=pageNum2;
            obj.type=1;
            console.log(obj)
            return obj;
        },
        visibleTime: 0,
        dataType: "json",
        visible:'hide',
        api: 'yg_baijiaapporder/orderRebate/selectRebateInfoByUserId',
        success:function(result){
            if(result.code=10000){
                var data=result.data;

                vm.userNum = data.userNum;   //用户的献金数
                vm.platformNum = data.platformNum;    //平台总献金
                vm.userYestodayFee = data.userYestodayFee;   //昨日收益
                vm.userTotalFee =data.userTotalFee;     //累计收益
                vm.platformYestodayFee1000 = data.platformYestodayFee1000;   //昨日昨日每万份献金净值

                    if(data.list.length>0){
                        //加载数据
                        loading2=false;
                        data.list.forEach(function(value,index){
                            value.updateTime=value.updateTime.split(' ')[0];
                            if((JSON.stringify(vm.shareCords).indexOf(JSON.stringify(value)))<0){
                                vm.shareCords.push(value);
                            }
                            //vm.shareCords.push(value);
                            console.log(value.money)
                        })
                    }else{
                        loading2=true;
                        //pageNum2=1;
                    //$(document.body).destroyInfinite();
                }








            }else{
                YDUI.dialog.toast(result.message, 'error', 1000);
            }
        }
    })
}

//收益记录   滚动加载信息
function infiniteSroll(){

    $("body").infinite().on("infinite", function () {
        var _index = $(".records_tab  a.textshow").index();
        console.log(_index);
        if(_index==0){
            if (loading1) return;
            loading1=true;
            pageNum1++;
            $('.weui-infinite-scroll-box').show();
            setTimeout(function () {
                loading1 = false;
                $('.weui-infinite-scroll-box').hide();
                viewEarnings();
            }, 1500);   //模拟延迟
        }else if(_index==1){
            if (loading2) return;
            loading2=true;
            pageNum2++;

            $('.weui-infinite-scroll-box').show();
            setTimeout(function () {
                loading2 = false;
                $('.weui-infinite-scroll-box').hide();
                viewShare();
            }, 1500);   //模拟延迟
        }

    });
}




//根据userId查看用户手机号
function getUserTel(){
    $.ajaxHandler({
        model:function(){
            var obj={};
            obj.userId= $.getPara('userId');
            console.log(obj)
            return obj;
        },
        visibleTime: 0,
        dataType: "json",
        visible:'hide',
        api: 'yg_baijiaapp/user/getUserEntityByIdNoPassword',
        success:function(res){
            if(res.code=10000){
                var data=res.data;
                vm.tel=data.userName;
            }else{
                YDUI.dialog.toast()
            }
        }

    })
}

$('#send-code-id').click(function(){
    sendCode();
})
//发送验证码
function sendCode(){
    $.ajaxHandler({
            model: function () {
                var obj = {};
                obj.mobile =vm.tel;
                console.log(obj);
                return obj;
            },
            visibleTime: 0,
            dataType: "json",
            api: 'yg_baijiaapp/CheckCode/sendVerificationCode',
            success: function (result) {
                console.log(result);
                if (result.code == 10000) {
                    YDUI.dialog.toast("短信发送成功",'success',1000);
                    SendTime("#send-code-id");
                } else if (result.code == 20112) {
                    YDUI.dialog.toast('短信发送失败','error',1000);
                } else if (result.code == 20000) {
                    YDUI.dialog.toast('请求失败','error',1000);
                } else if (result.code == 29901) {
                    YDUI.dialog.toast('无效参数','error',1000);
                } else {
                    YDUI.dialog.toast(result.message,"warning");
                }

            }
        }
    );
}

var wait = 60;//短信时间
function SendTime(parameter) {
    var Send = $("" + parameter + "");
    if (wait == 0) {
        Send.html("发送验证码");
        wait = 60;
        //$("#send-code-id").removeAttr("disabled");
    } else {
        Send.html("重新发送(" + wait + ")s");
        wait--;
        setTimeout(function () {
            SendTime(parameter);
        }, 1000);
    }
}

$('.confirmBtn').click(function(){
    console.log(13)
    rollout();
})
//转出献金
function rollout(){
    if ($("#code-id").val() == ""||$('#outNum').val()=="") {
        YDUI.dialog.toast("输入不能为空",1000);
    }else if($('#outNum').val()<=0){
        YDUI.dialog.toast("转出数量需大于0",1000);
    }else if($('#outNum').val()>vm.userNum){
        YDUI.dialog.toast("转出数量需小于等于用户当前份数",1000);
    }else if($('#outNum').val().indexOf('.')>0){
        YDUI.dialog.toast("转出份数必须是整数",1000);
    }
    else{
        $.ajaxHandler({
            model:function(){
                var obj={};
                obj.mobile=vm.tel;
                obj.code=$("#code-id").val();
                obj.num=$("#outNum").val();
                console.log(obj);
                return obj;
            },
            visibleTime: 0,
            dataType: "json",
            visible:'hide',
            api: 'yg_baijiaapporder/orderRebate/subUserRebateNum',
            success:function(res){

                if(res.code==10000){
                    $('.operateBtns').show();
                    $('.rolloutOpe').toggle();
                    $('#fullbg').hide();
                    $('body').css({
                        overflow:'auto'
                    })
                    YDUI.dialog.toast('转出成功','success',1000);
                    //转出成功后重新刷新页面
                    setTimeout(function() {
                        window.location.reload()
                    },2000)

                }else if(res.code==20193){
                    $('.operateBtns').show();
                    $('.rolloutOpe').toggle();
                    $('#fullbg').show();
                    $('body').css({
                        overflow:'hidden'
                    })
                    $('.prompt').show();
                }else{
                    YDUI.dialog.toast(res.message,'error',1000);
                }

            }

        })
    }

}


//下载雨润app     与app交互
function jumpToApp() {
    var u = navigator.userAgent;
    var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
    var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    var url='http://appd.closfeel.cn/hmkj/hmkj0000/appd.html';
    var scheme='';
    if(isAndroid){
        //alert("安卓系统");
        JSAndroid.toJumpOtherApp(url);
    }else if(isiOS){
        //url='1330075403';
        //scheme='wistomStore';
        //window.webkit.messageHandlers.shareBooking.postMessage(this.data.activity.id,this.data.activity.join_company_id);
        toJumpOtherApp(url);
    }


}



//献金提现
function toCash(){
    var u = navigator.userAgent;
    var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
    var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    var url='';
    if(isAndroid){
        //alert("安卓系统");
        JSAndroid.toRemainingSum();
    }else if(isiOS){
        url='1330075403'
        //alert("苹果系统");
        //window.webkit.messageHandlers.shareBooking.postMessage(this.data.activity.id,this.data.activity.join_company_id);
        toRemainingSum();
    }
}

